package com.leetcode.string;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author Dennis Li
 * @date 2020/9/8 10:39
 */
public class LengthOfLongestSubstring_3 {
    public int lengthOfLongestSubstring2(String s) {
        Queue<Character> queue = new LinkedList<>();
        boolean[] showed = new boolean[256];
        int max = 0;
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (!showed[ch]) {
                queue.offer(ch);
                showed[ch] = true;
                max = Math.max(max, queue.size());
            } else {
                while (queue.peek() != ch) {
                    Character poll = queue.poll();
                    showed[poll] = false;
                }
                queue.offer(queue.poll());
            }
        }
        return max;
    }
}
